<?php
/**
 * Created by PhpStorm.
 * User: osxfal
 * Date: 2019/2/23
 * Time: 下午8:01
 */
namespace app\index\controller;

use think\Controller;
use app\depend\CarLifeCurl;
use think\cache\driver\Redis;
use think\Session;

/* 短信验证码发送 */
class MessagesApi extends Controller{

    //短信信息组装， 调用接口发送
    public function msg_info(){
        $cached = new Redis();
        $accesskey = 'e2IuSrINvrucfk6T';
        $secret = 'ztlOnZ2C3LMzFs3PUVysSLJEsfkAoxO1';
        $sign='18685';  //微用钱
        $templateId = '37804';   //模板id
        $mobile = input('post.mobile'); //接收手机用户
        $mobile = trim($mobile);
        $data = [];
        
        
        /* 保存用户信息 到 session */
        $rand_code = mt_rand(10000,99999);  // 5位随机码
        $data[$mobile] = $mobile;
        $data['rand_code'] = $rand_code;


        /*使用 redis 做为缓存*/
        $cached->set('user_info',$data);


        $content = $rand_code;  //纯数字的 ， 不需要  urlencode($rand_code);

        //信息数组
        $data = [];
        $data['accesskey'] = $accesskey;
        $data['secret'] = $secret;
        $data['sign']= $sign;
        $data['templateId'] = $templateId;
        $data['mobile'] = $mobile;
        $data['content'] = $content;
        
        $ret = CarLifeCurl::curlPost($data);

//        dump($ret);

        /* 数据返回：
            code  Int  请求状态数 {0表示请求成功，非0表示请求失败;}
            msg    String  状态说明
            smUuid  String 短信的唯一码  验证码?
        */

        if($ret['code'] == 0){
            $box =[];
            $box['result'] = 1;
            $box['tips'] = '短信发送成功！';
            $box = json_encode($box,320);
            exit($box);  //返回验证码
        }
        

    }

    public function check_verified_code($msg_code){
        $user_info = session('user_info');
        if($user_info['rand_code'] == $msg_code){
            return true;
        }else{
            return false;
        }

    }//func


    /* 展示给渠道商看的个性化网址， 根据token 展示对应的渠道商数据
        表： go_admin
    */
    public function statistic_list(){
        //接收参数
        $tok = input('get.token');
        $tok = trim($tok);
        if($_POST){
            $stime = input('post.starttime');
            $etime = input('post.endtime');
            $tok = input('post.tok');

            $stime = strtotime($stime);
            $etime = strtotime($etime);

            $where = [];
            $where['time'] = ['BETWEEN',[$stime,$etime]];
//            $where['token'] = $tok;

            //根据 token 找对应的注册用户
            $res = db('admin')->field('uid')->where(['token'=>$tok])->find();
            $where['diamond'] = $res['uid'];
            $members = db('member')->where($where)->select();
            $where['is_name'] = 1;
            $is_name_num = db('member')->where($where)->count();
            $this->assign([
                'members'=>$members,
                'total'=>count($members),
                'is_name_num'=>$is_name_num,
                'token'=>$tok
            ]);

            exit($this->fetch(TEM_HTML.'mymembers.html'));

        }



        //根据 token 找对应的注册用户
        $res = db('admin')->field('uid')->where(['token'=>$tok])->find();

        $members = db('member')->where(['diamond'=>$res['uid']])->select();
        $w = [];
        $w['diamond'] = $res['uid'];
        $w['is_name'] = 1;
        $is_name_num = db('member')->where($w)->count();


        $this->assign([
            'members'=>$members,
            'total'=>count($members),
            'is_name_num'=>$is_name_num,
            'token'=>$tok
        ]);

        exit($this->fetch(TEM_HTML.'mymembers.html'));

    }

    
}

